home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL13.TXT < prev    next >
Text File  |  1986-04-05  |  5KB  |  227 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 60
  2.  
  3.  
  4. TURBO-LESSON 13:  STRINGS
  5.  
  6. OBJECTIVES - In this lesson, you will learn about:
  7.  
  8. 1.  Strings
  9. 2.  String replacement statement
  10. 3.  Predefined string function, LENGTH
  11.  
  12.  
  13. 1.  Strings.
  14.  
  15. You have already seen some Pascal strings in the WriteLn 
  16. statements of earlier lessons.  
  17.  
  18. WriteLn('This is a string.');
  19.  
  20. It is often convenient to store strings as variables or 
  21. constants.
  22.  
  23. A string constant may be defined in the CONST section:
  24.  
  25. CONST   String_1  =  'TURBO-LESSONS';
  26.  
  27. String variables must be declared in the VAR section.  The form 
  28. of the declaration is:
  29.  
  30. VAR    First_Name : String[12];
  31.  
  32. This sets up storage for a variable named First_Name which can 
  33. store a string up to 12 characters long.   
  34.  
  35.  
  36. 2.  String replacement statement.
  37.  
  38. The replacement statement for strings is:
  39.  
  40. String_Name := (string expression);
  41.  
  42. ##### DO:
  43.  
  44. Examine PROG13.  Notice the following:
  45.  
  46. A string constant, S_Test is given the value 'Test String' in the 
  47. CONST declaration section.
  48.  
  49. Several string variables are defined in the VAR section.  
  50. î
  51. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 61
  52.  
  53.  
  54. ##### DO:
  55.  
  56. Run the program.
  57.  
  58. What happens when an attempt is made to store too long a string 
  59. in a string variable?
  60.  
  61. ##### DO:
  62.  
  63. Add the following to the program:
  64.  
  65. S5 := S_Test;
  66. WriteLn(S5);
  67.  
  68. Run the program.  
  69.  
  70. How many characters of S5 are printed?
  71.  
  72. ##### DO:
  73.  
  74. Modify WriteLn(S5) to:
  75.  
  76. WriteLn('[', S5, ']');
  77.  
  78. Run the program.  How many characters of S5 are printed?
  79.  
  80. DEBUGGING NOTE:  When working with strings, you may find it 
  81. helpful to print some kind of marker before and after a string to 
  82. help "see" the occurrences of the character, blank.
  83.  
  84. You have seen what happens when storing 'Test String' in too 
  85. short a variable:  S3 holds 'Tes',  S8 holds 'Test Str'.
  86.  
  87. What happens when a string is stored in a variable that is larger 
  88. than needed?  Are blanks added?
  89.  
  90. ##### DO:
  91.  
  92. Modify the WriteLn(S14) to bracket S14 (like you did above with 
  93. S5) and run the program.   
  94.  
  95. How many characters of S14 were printed?
  96.  
  97. Were extra blanks added?    (More on this later.)
  98. î
  99. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 62
  100.  
  101.  
  102. ##### DO:
  103.  
  104. Look at PROG13A.    
  105.  
  106. WriteLn(S8[I]);
  107.  
  108. Notice the use of the square brackets in the statement above.  
  109. This is a way to refer to a specific character in a string.
  110.  
  111. S8[2] means the 2nd character in the string, S8.
  112.  
  113. NOTE: SQUARE BRACKETS ARE USED IN TWO DIFFERENT WAYS WITH 
  114. STRINGS.  WHEN DECLARING VARIABLES, THE BRACKETS ENCLOSE THE 
  115. MAXIMUM LENGTH OF THE STRING.  IN PROCESSING STATEMENTS, THE 
  116. NUMBER IN THE BRACKETS DESIGNATE A PARTICULAR CHARACTER IN A 
  117. STRING.
  118.  
  119. ##### DO:
  120.  
  121. Run the program, using 2 as position number.
  122.  
  123. Try 5 as an input.  What character was stored as the 5th 
  124. character of S8?
  125.  
  126. The string stored in S8, 'TURBO', is 5 characters long 
  127. but S8 is 8 characters long.
  128.  
  129. What characters, if any, are stored in S8[6], S8[7], and 
  130. S8[8]?
  131.  
  132. ##### DO:
  133.  
  134. Run the program with input values of 6, 7, and 8.
  135.  
  136. What characters were printed?
  137.  
  138. ##### DO:
  139.  
  140. Add the following statement as the first statement in the BEGIN 
  141. END block:
  142.  
  143. S8 := '12345678';
  144.  
  145. Run the program again, using 6, 7, and 8 as input.
  146.  
  147. What do you conclude about "unused" positions in a string?
  148.  
  149. Before you are prompted to enter the "position number", S8 is 
  150. printed.  
  151.  
  152. Do positions 6, 7, 8 of the string print?   Why?
  153. î
  154. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 63
  155.  
  156.  
  157. ##### DO:
  158.  
  159. Run the program once more, this time use 0 as the input value.
  160.  
  161. What character printed?
  162.  
  163. If you look up this character, clubs symbol, in the ASCII chart 
  164. in your BASIC manual, you find that it is the character
  165. associated with ASCII value 8.
  166.  
  167. String variables in TURBO are one character longer than the 
  168. maximum length you specify.  This extra character is at the 
  169. beginning of the string, at position 0, and always contains the 
  170. length of the string stored.  
  171.  
  172. So why isn't the length stored as a number?  
  173.  
  174. Storing the length information as a character makes position 0 
  175. the same type as the other characters in the string.  
  176.  
  177.  
  178. 3.  Predefined string function, LENGTH.
  179.  
  180. Because the length of a string is often needed in processing, 
  181. the function, LENGTH, has been provided for that purpose.
  182.  
  183. ##### DO:
  184.  
  185. Add the following statement after the UNTIL statement:
  186.  
  187. WriteLn('Length of string: ', LENGTH(S8) );
  188.  
  189. Run the program.  
  190.  
  191. There is also another way to get the ASCII value of the character 
  192. at position 0 of a string.  
  193.  
  194. ##### DO: 
  195.  
  196. Add the following statement just before the END:
  197.  
  198. WriteLn('ASCII value: ', ORD(S8[0]) );
  199.  
  200. Run the program.  
  201.  
  202. Does the ASCII value agree with the value obtained with the 
  203. LENGTH function?
  204. î
  205. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 64
  206.  
  207.  
  208. ##### DO:
  209.  
  210. Just before the END, insert:
  211.  
  212. FOR I := 0 to LENGTH(S8) DO
  213.    WriteLn('Position ', I:2, ':  ',S8[I]);
  214.  
  215. Run the program.
  216.  
  217. ##### DO:
  218.  
  219. Change the CONST declaration to:
  220.  
  221. S_Test = 'OK'; 
  222.  
  223. Run the program.
  224.  
  225. Also try 'Wake Up' for S_Test.   
  226. î
  227.